
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.ext.cod &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.ext.cod</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.ext.cod</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding: utf-8</span>
<span class="c1"># Copyright (c) Pymatgen Development Team.</span>
<span class="c1"># Distributed under the terms of the MIT License.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">This module provides classes to interface with the Crystallography Open</span>
<span class="sd">Database. If you use data from the COD, please cite the following works (as</span>
<span class="sd">stipulated by the COD developers)::</span>

<span class="sd">    Merkys, A., Vaitkus, A., Butkus, J., Okulič-Kazarinas, M., Kairys, V. &amp;</span>
<span class="sd">    Gražulis, S. (2016) &quot;COD::CIF::Parser: an error-correcting CIF parser for</span>
<span class="sd">    the Perl language&quot;. Journal of Applied Crystallography 49.</span>

<span class="sd">    Gražulis, S., Merkys, A., Vaitkus, A. &amp; Okulič-Kazarinas, M. (2015)</span>
<span class="sd">    &quot;Computing stoichiometric molecular composition from crystal structures&quot;.</span>
<span class="sd">    Journal of Applied Crystallography 48, 85-91.</span>

<span class="sd">    Gražulis, S., Daškevič, A., Merkys, A., Chateigner, D., Lutterotti, L.,</span>
<span class="sd">    Quirós, M., Serebryanaya, N. R., Moeck, P., Downs, R. T. &amp; LeBail, A.</span>
<span class="sd">    (2012) &quot;Crystallography Open Database (COD): an open-access collection of</span>
<span class="sd">    crystal structures and platform for world-wide collaboration&quot;. Nucleic</span>
<span class="sd">    Acids Research 40, D420-D427.</span>

<span class="sd">    Grazulis, S., Chateigner, D., Downs, R. T., Yokochi, A. T., Quiros, M.,</span>
<span class="sd">    Lutterotti, L., Manakova, E., Butkus, J., Moeck, P. &amp; Le Bail, A. (2009)</span>
<span class="sd">    &quot;Crystallography Open Database – an open-access collection of crystal</span>
<span class="sd">    structures&quot;. J. Appl. Cryst. 42, 726-729.</span>

<span class="sd">    Downs, R. T. &amp; Hall-Wallace, M. (2003) &quot;The American Mineralogist Crystal</span>
<span class="sd">    Structure Database&quot;. American Mineralogist 88, 247-250.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">requests</span>
<span class="kn">import</span> <span class="nn">subprocess</span>
<span class="kn">from</span> <span class="nn">monty.dev</span> <span class="kn">import</span> <span class="n">requires</span>
<span class="kn">from</span> <span class="nn">monty.os.path</span> <span class="kn">import</span> <span class="n">which</span>

<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">from</span> <span class="nn">pymatgen.core.composition</span> <span class="kn">import</span> <span class="n">Composition</span>
<span class="kn">from</span> <span class="nn">pymatgen.core.structure</span> <span class="kn">import</span> <span class="n">Structure</span>

<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Shyue Ping Ong&quot;</span>
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s2">&quot;Copyright 2012, The Materials Project&quot;</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="s2">&quot;1.0&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s2">&quot;Shyue Ping Ong&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s2">&quot;shyuep@gmail.com&quot;</span>


<div class="viewcode-block" id="COD"><a class="viewcode-back" href="../../../pymatgen.ext.cod.html#pymatgen.ext.cod.COD">[docs]</a><span class="k">class</span> <span class="nc">COD</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    An interface to the Crystallography Open Database.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Blank __init__. No args required.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">pass</span>

<div class="viewcode-block" id="COD.query"><a class="viewcode-back" href="../../../pymatgen.ext.cod.html#pymatgen.ext.cod.COD.query">[docs]</a>    <span class="k">def</span> <span class="nf">query</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sql</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Perform a query.</span>

<span class="sd">        :param sql: SQL string</span>
<span class="sd">        :return: Response from SQL query.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">r</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">check_output</span><span class="p">([</span><span class="s2">&quot;mysql&quot;</span><span class="p">,</span> <span class="s2">&quot;-u&quot;</span><span class="p">,</span> <span class="s2">&quot;cod_reader&quot;</span><span class="p">,</span> <span class="s2">&quot;-h&quot;</span><span class="p">,</span>
                                     <span class="s2">&quot;www.crystallography.net&quot;</span><span class="p">,</span> <span class="s2">&quot;-e&quot;</span><span class="p">,</span>
                                     <span class="n">sql</span><span class="p">,</span> <span class="s2">&quot;cod&quot;</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">r</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s2">&quot;utf-8&quot;</span><span class="p">)</span></div>

<div class="viewcode-block" id="COD.get_cod_ids"><a class="viewcode-back" href="../../../pymatgen.ext.cod.html#pymatgen.ext.cod.COD.get_cod_ids">[docs]</a>    <span class="nd">@requires</span><span class="p">(</span><span class="n">which</span><span class="p">(</span><span class="s2">&quot;mysql&quot;</span><span class="p">),</span> <span class="s2">&quot;mysql must be installed to use this query.&quot;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">get_cod_ids</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">formula</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Queries the COD for all cod ids associated with a formula. Requires</span>
<span class="sd">        mysql executable to be in the path.</span>

<span class="sd">        Args:</span>
<span class="sd">            formula (str): Formula.</span>

<span class="sd">        Returns:</span>
<span class="sd">            List of cod ids.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># TODO: Remove dependency on external mysql call. MySQL-python package does not support Py3!</span>

        <span class="c1"># Standardize formula to the version used by COD.</span>

        <span class="n">sql</span> <span class="o">=</span> <span class="s1">&#39;select file from data where formula=&quot;- </span><span class="si">%s</span><span class="s1"> -&quot;&#39;</span> <span class="o">%</span> \
            <span class="n">Composition</span><span class="p">(</span><span class="n">formula</span><span class="p">)</span><span class="o">.</span><span class="n">hill_formula</span>
        <span class="n">text</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="n">cod_ids</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="n">text</span><span class="p">:</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;(\d+)&quot;</span><span class="p">,</span> <span class="n">l</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">m</span><span class="p">:</span>
                <span class="n">cod_ids</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)))</span>
        <span class="k">return</span> <span class="n">cod_ids</span></div>

<div class="viewcode-block" id="COD.get_structure_by_id"><a class="viewcode-back" href="../../../pymatgen.ext.cod.html#pymatgen.ext.cod.COD.get_structure_by_id">[docs]</a>    <span class="k">def</span> <span class="nf">get_structure_by_id</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cod_id</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Queries the COD for a structure by id.</span>

<span class="sd">        Args:</span>
<span class="sd">            cod_id (int): COD id.</span>
<span class="sd">            kwargs: All kwargs supported by</span>
<span class="sd">                :func:`pymatgen.core.structure.Structure.from_str`.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A Structure.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;http://www.crystallography.net/cod/</span><span class="si">%s</span><span class="s2">.cif&quot;</span> <span class="o">%</span> <span class="n">cod_id</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">Structure</span><span class="o">.</span><span class="n">from_str</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">text</span><span class="p">,</span> <span class="n">fmt</span><span class="o">=</span><span class="s2">&quot;cif&quot;</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>

<div class="viewcode-block" id="COD.get_structure_by_formula"><a class="viewcode-back" href="../../../pymatgen.ext.cod.html#pymatgen.ext.cod.COD.get_structure_by_formula">[docs]</a>    <span class="nd">@requires</span><span class="p">(</span><span class="n">which</span><span class="p">(</span><span class="s2">&quot;mysql&quot;</span><span class="p">),</span> <span class="s2">&quot;mysql must be installed to use this query.&quot;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">get_structure_by_formula</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">formula</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Queries the COD for structures by formula. Requires mysql executable to</span>
<span class="sd">        be in the path.</span>

<span class="sd">        Args:</span>
<span class="sd">            cod_id (int): COD id.</span>
<span class="sd">            kwargs: All kwargs supported by</span>
<span class="sd">                :func:`pymatgen.core.structure.Structure.from_str`.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A list of dict of the format</span>
<span class="sd">            [{&quot;structure&quot;: Structure, &quot;cod_id&quot;: cod_id, &quot;sg&quot;: &quot;P n m a&quot;}]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">structures</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">sql</span> <span class="o">=</span> <span class="s1">&#39;select file, sg from data where formula=&quot;- </span><span class="si">%s</span><span class="s1"> -&quot;&#39;</span> <span class="o">%</span> \
              <span class="n">Composition</span><span class="p">(</span><span class="n">formula</span><span class="p">)</span><span class="o">.</span><span class="n">hill_formula</span>
        <span class="n">text</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="n">text</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="n">text</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">l</span><span class="o">.</span><span class="n">strip</span><span class="p">():</span>
                <span class="n">cod_id</span><span class="p">,</span> <span class="n">sg</span> <span class="o">=</span> <span class="n">l</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\t</span><span class="s2">&quot;</span><span class="p">)</span>
                <span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;http://www.crystallography.net/cod/</span><span class="si">%s</span><span class="s2">.cif&quot;</span>
                                 <span class="o">%</span> <span class="n">cod_id</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">s</span> <span class="o">=</span> <span class="n">Structure</span><span class="o">.</span><span class="n">from_str</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">text</span><span class="p">,</span> <span class="n">fmt</span><span class="o">=</span><span class="s2">&quot;cif&quot;</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
                    <span class="n">structures</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s2">&quot;structure&quot;</span><span class="p">:</span> <span class="n">s</span><span class="p">,</span> <span class="s2">&quot;cod_id&quot;</span><span class="p">:</span> <span class="nb">int</span><span class="p">(</span><span class="n">cod_id</span><span class="p">),</span>
                                       <span class="s2">&quot;sg&quot;</span><span class="p">:</span> <span class="n">sg</span><span class="p">})</span>
                <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                    <span class="kn">import</span> <span class="nn">warnings</span>
                    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">Structure.from_str failed while parsing CIF file:</span><span class="se">\n</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">r</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
                    <span class="k">raise</span>

        <span class="k">return</span> <span class="n">structures</span></div></div>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.ext.cod</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>